$(document).ready(function(e) {   
	if ($('.up_trigger').hasClass('up_activer'))
	{
		$('.up_trigger').bind('click',remove);
		$('.down_trigger').bind('click',switchxplike);	
	}
	if ($('.down_trigger').hasClass('down_activer'))
	{
		$('.down_trigger').bind('click',remove);
		$('.up_trigger').bind('click',switchxplike);	
	}
	$('.xplikelogin').bind('click',function () {
		$.post(baseurl +'index.php',{option:'com_ilike',task:'login',format:'raw',username:$('#username').val(),password:$('#password').val()},function (html) {
			if (html == '1')
			{
				location.reload(); 
			}
			else
			{
				$('#wrongpass').show();
				setTimeout(function () {$('#wrongpass').fadeOut(2000)},5000);
			}
		});
	});
	$('.xplike').click(like);
	$('.xpdislike').click(dislike);
});
function popuplogin()
{
	$('.xplike_overlay').show();
	$('#xplikebox').show();
}
function caculatebar(value)
{
	var currentlike = parseInt($('.thumbs_up').html());
	var currentdislike = parseInt($('.thumbs_down').html());
	var total = currentlike + currentdislike;
	$('.xplikebar-like').css('width',(currentlike/total)*100+'%');
	$('.xplikebar-dislike').css('width',(currentdislike/total)*100+'%');
}
like = function () {
	var element= $(this);
	$.post(baseurl +'index.php',{option:'com_ilike',task:'like',format:'raw',component:element.attr('data-option'),id:element.attr('data-id')},function (html) {
		element.addClass('up_activer').unbind('click');
		element.bind('click',remove);
		$('.xpdislike').unbind('click').bind('click',switchxplike);
		$('.thumbs_up').html(parseInt($('.thumbs_up').html())+1);
		$('#xpnoitice').show().html('You like it!');
		$('.xplikeremove').show();
		caculatebar();
	});		
}
dislike= function () {
	var element= $(this);
	$.post(baseurl +'index.php',{option:'com_ilike',task:'dislike',format:'raw',component:element.attr('data-option'),id:element.attr('data-id')},function (html) {
		element.addClass('down_activer').unbind('click');
		element.bind('click',remove);
		$('.xplike').unbind('click').bind('click',switchxplike)
		$('.thumbs_down').html(parseInt($('.thumbs_down').html())+1);
		$('#xpnoitice').show().html('You dislike it!');
		$('.xplikeremove').show();
		caculatebar();
	});		
}
remove = function () {
	var element= $(this);
	element.unbind('click');
	$.post(baseurl +'index.php',{option:'com_ilike',task:'remove',format:'raw',component:element.attr('data-option'),id:element.attr('data-id')},function (html) {
		if (html == '1')
		{
			$('.thumbs_up').html(parseInt($('.thumbs_up').html())-1);
			$('.up_trigger').removeClass('up_activer').addClass('xplike');
			$('.down_trigger').removeClass('down_activer').addClass('xpdislike');
		}
		else
		{
			$('.thumbs_down').html(parseInt($('.thumbs_down').html())-1);
			$('.up_trigger').removeClass('up_activer').addClass('xplike');
			$('.down_trigger').removeClass('down_activer').addClass('xpdislike');
		}
		$('.xplike').unbind('click').bind('click',like);	
		$('.xpdislike').unbind('click').bind('click',dislike);					
		$('#xpnoitice').fadeOut();
		caculatebar();
	});		
}
switchxplike = function () {
	var element= $(this);
	element.unbind('click');
	$.post(baseurl +'index.php',{option:'com_ilike',task:'switchxplike',format:'raw',component:element.attr('data-option'),id:element.attr('data-id')},function (html) {
		if (html == '1')
		{
			//like active	
			$('.thumbs_up').html(parseInt($('.thumbs_up').html())+1);
			$('.thumbs_down').html(parseInt($('.thumbs_down').html())-1);
			$('.up_trigger').addClass('up_activer').addClass('xplike').unbind('click').bind('click',remove);
			$('.down_trigger').removeClass('down_activer').addClass('xpdislike').unbind('click').bind('click',switchxplike);
			$('#xpnoitice').show().html('Bạn thích điều này!');
		}
		else
		{
			$('.thumbs_up').html(parseInt($('.thumbs_up').html())-1);
			$('.thumbs_down').html(parseInt($('.thumbs_down').html())+1);
			$('.up_trigger').removeClass('up_activer').addClass('xplike').unbind('click').bind('click',switchxplike);
			$('.down_trigger').addClass('down_activer').addClass('xpdislike').unbind('click').bind('click',remove);
			$('#xpnoitice').show().html('Bạn không thích điều này!');
		}
		caculatebar();
	});
}